package com.liyunhan.SortAlgorithm;

import java.util.ArrayList;

/**
 * @Description: 希尔排序 , 采用每次将增量除以2并向下取整.
 * @Author: Liyunhan
 * @Date: 2021/10/3 22:20
 */
public class ShellSort extends AlgorithmAbs {

    /**
     * 构造器
     *
     * @param arrayList
     */
    public ShellSort(ArrayList<Double> arrayList) {
        super(arrayList);
    }


    /**
     * 算法执行
     */
    @Override
    public void executeAlgorithm(int...ints) {
        //step每次分割的步长
        for (int step = arrayList.size() / 2; step > 0; step = step / 2) {
            for (int i = step; i < arrayList.size(); i++) {
                //取出以step分割出的当前列的序列进行直接插入排序
                for (int j = i - step; j >= 0; j = j - step) {
                    //交换
                    if (arrayList.get(j) > arrayList.get(j + step)) {
                        double temp = arrayList.get(j);
                        arrayList.set(j, arrayList.get(j + step));
                        arrayList.set(j + step, temp);
                    }

                }

            }
        }
    }
}
